from sqlalchemy.orm import Session
from . import models, schemas

def get_custom_reply(db: Session, custom_reply_id: int):
    return db.query(models.CustomReply).filter(models.CustomReply.id == custom_reply_id).first()

def get_custom_replies(db: Session, skip: int = 0, limit: int = 100):
    return db.query(models.CustomReply).offset(skip).limit(limit).all()

def create_custom_reply(db: Session, custom_reply: schemas.CustomReplyCreate):
    db_custom_reply = models.CustomReply(**custom_reply.dict())
    db.add(db_custom_reply)
    db.commit()
    db.refresh(db_custom_reply)
    return db_custom_reply

def update_custom_reply(db: Session, custom_reply_id: int, custom_reply: schemas.CustomReplyUpdate):
    db_custom_reply = db.query(models.CustomReply).filter(models.CustomReply.id == custom_reply_id).first()
    if db_custom_reply:
        for var, value in vars(custom_reply).items():
            setattr(db_custom_reply, var, value) if value else None
        db.add(db_custom_reply)
        db.commit()
        db.refresh(db_custom_reply)
    return db_custom_reply

def delete_custom_reply(db: Session, custom_reply_id: int):
    db_custom_reply = db.query(models.CustomReply).filter(models.CustomReply.id == custom_reply_id).first()
    if db_custom_reply:
        db.delete(db_custom_reply)
        db.commit()
    return db_custom_reply